float4x4 World;
float4x4 View;
float4x4 Projection;
texture BasicTexture;
sampler BasicTextureSampler = sampler_state {
texture = <BasicTexture>;
};
bool TextureEnabled = false;
float3 DiffuseColor = float3(1, 1, 1);
float3 AmbientColor = float3(.1, .1, .1);
float3 LightDirection = float3(1, 0, -1);
float3 LightColor = float3(1, 1, 1);
float SpecularPower = 32;
float3 SpecularColor = float3(1, 1, 1);
float3 CameraPosition;
// TODO: add effect parameters here.

struct VertexShaderInput
{
    float4 Position : POSITION0;
    float2 UV : TEXCOORD0;
    float3 Normal : NORMAL0;

    // TODO: add input channels such as texture
    // coordinates and vertex colors here.
};

struct VertexShaderOutput
{

    float4 Position : POSITION0;
    float2 UV : TEXCOORD0;
    float3 Normal : TEXCOORD1;
    float3 ViewDirection : TEXCOORD2;

    // TODO: add vertex shader outputs such as colors and texture
    // coordinates here. These values will automatically be interpolated
    // over the triangle, and provided as input to your pixel shader.
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;
    
    float4 worldPosition = mul(input.Position, World);
	float4x4 viewProjection = mul(View, Projection);
	output.Position = mul(worldPosition, viewProjection);
    output.UV = input.UV;
    output.Normal = mul(input.Normal, World);
    output.ViewDirection = worldPosition - CameraPosition;
    

    // TODO: add your vertex shader code here.

    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
   
	float3 color = DiffuseColor;
	// Texture if necessary
	if (TextureEnabled)
	color *= tex2D(BasicTextureSampler, input.UV);
	// Start with ambient lighting
	float3 lighting = AmbientColor;
	float3 lightDir = normalize(LightDirection);
	float3 normal = normalize(input.Normal);
	// Add lambertian lighting
	lighting += saturate(dot(lightDir, normal)) * LightColor;
	float3 refl = reflect(lightDir, normal);
	float3 view = normalize(input.ViewDirection);
	// Add specular highlights
	lighting += pow(saturate(dot(refl, view)), SpecularPower) *
	SpecularColor;
	// Calculate final color
	float3 output = saturate(lighting) * color;
	return float4(output, 1);
}

technique Technique1
{
    pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_1_1 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}
